<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 数据统计
                </el-breadcrumb-item>
                <el-breadcrumb-item>柱状图</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <span style="display: inline-block; margin-right: 10px;">请选择数据类型: </span>
                <el-select v-model="query.type" placeholder="请选择数据类型" @change="typeChange">
                    <el-option v-for="item in items" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </div>
            <schart style="padding-top: 30px" ref="bar" class="schart" canvasId="bar" :options="options" :key="updateKey"></schart>
        </div>
    </div>
</template>

<script>
import Schart from "vue-schart";
import {reactive, ref} from "vue";
import { getOrderSevenData, fenToYuan, gToKg } from '../../api'
export default {
    name: "orderSevenData",
    components: { Schart },
    setup() {
        const updateKey = ref(0);
        const query = reactive({
            type: 0
        });
        const items = [
            {id: 0, name: "订单数"},
            {id: 1, name: "订单金额"},
            {id: 2, name: "废品重量"},
        ];
        const options = {
            type: "bar",
            title: {
                text: "最近7天数据柱状图",
            },
            xRorate: 25,
            labels: ['2021/01/12', '2021/01/13', '2021/01/14', '2021/01/15', '2021/01/16', '2021/01/17', '2021/01/18'],
            datasets: [
                {
                    label: "订单数",
                    data: [0, 1, 2, 3, 4, 5 ,6]
                }
            ],
        };

        let myData = [];
        const fetchBaseData = () => {
            getOrderSevenData().then(res => {
                options.labels = res.data.map(e => {
                    return e.day;
                });

                myData.push({
                    label: "订单数",
                    data: res.data.map(e => {
                        return e.count;
                    })
                });
                myData.push({
                    label: "订单金额(￥)",
                    fillColor: "rgba(241,84,15,0.5)",
                    data: res.data.map(e => {
                        return fenToYuan(e.amount);
                    })
                });
                myData.push({
                    label: "废品重量(KG)",
                    fillColor: "rgba(44,104,241,0.5)",
                    data: res.data.map(e => {
                        return gToKg(e.weight);
                    })
                });
                console.log(myData);
                options.datasets[0] = myData[0];
                updateKey.value = updateKey.value + 1;
            })
        };
        fetchBaseData();

        const typeChange = (e) => {
            options.datasets[0] = myData[query.type];
            updateKey.value = updateKey.value + 1;
        };

        return {
            options,
            items,
            updateKey,
            query,
            typeChange
        };
    },
};
</script>

<style scoped>
.el-row {
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 100px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
    color: rgb(242, 94, 67);
}

.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 2px solid #ccc;
    margin-bottom: 20px;
}

.user-avator {
    width: 120px;
    height: 120px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 50px;
    flex: 1;
    font-size: 14px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 14px;
    color: #999;
    line-height: 25px;
}

.user-info-list span {
    margin-left: 70px;
}

.member span {
    display: inline-block;
    padding-top: 8px;
    padding-bottom: 8px;
}
.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}
</style>
